;
(function() {
	var skill = {
		bindCardToAch: function(card_id, ach_id) {
			var ach = this._getAch(ach_id);
			return ach._bindCard(card_id);
		},
		_getCardIdList: function() {
			var card_list = this.card_list;
			if (!Array.isArray(card_list)) {
				card_list = this.card_list = [];
				this.save();
			}
			return card_list;
		},
		getCardList: function(state) {
			state = ~~state;
			var card_list = this._getCardIdList().map(function(card_id) {
				return CardCon.get(card_id);
			});
			if (state) {
				card_list = card_list.filter(function(card) {
					return card.state === state;
				});
			}
			return card_list;
		},
		getCardListByBusId: function(bus_id, state) {
			state = ~~state;
			var result = [];
			this._getCardIdList().filter(function(card_id) {
				if (CardCon.cardId_has_bus(card_id, bus_id)) {
					var card = CardCon.get(card_id);
					if (state && card.state !== state) {
						return
					}
					result.push(card);
				}
			});
			return result;
		},
		_getCard: function(card_id) {
			var card_list = this._getCardIdList();
			if (card_list.indexOf(card_id) === -1) {
				throwCusError("该会员卡不属于指定用户")
			}
			return CardCon.get(card_id);
		},
		getUseAbleCardListForAch: function(ach_id, is_force /*强行取订单*/ ) {
			if (is_force) {
				var ach = AchCon.get(ach_id);
				ach.getCash();
			} else {
				ach = this._getAch(ach_id);
			}
			var card_list = this.getCardList(CardCon.STATE.可用).filter(function(card) {
				return card._canUseForAch(ach);
			});
			return card_list;
		},
		//只能领取增加，不可删除不可修改
		addCardFromCardFactory: function(card_factory_id) {
			var card_factory = CardFactoryCon.get(card_factory_id);
			if (!card_factory) {
				throwCusError("找不到指定会员卡可领取")
			}
			if (card_factory.info.need_buy) {
				throwCusError("此会员卡需要通过购买的途径获得");
			}
			var card = card_factory.toCard(this._id);
			this._getCardIdList().unshift(card._id);
			return card;
		},
		_addCardFromCardFactoryNeedBuyCb: function(ach_id, card_factory_id) {
			var card_factory = CardFactoryCon.get(card_factory_id);
			if (!card_factory) {
				throwCusError("找不到指定会员卡可领取")
			}
			//对订单信息做判断
			var ach = AchCon.get(ach_id);
			if (ach.isEndPay()) {
				if (ach.info.user_id !== this._id) { //一个不重要但是有可能发生数据拦截的状态
					throwCusError("订单与用户不匹配")
				}
				if (ach.info.goods_info.some(function(goods_and_num) {
						if (goods_and_num.goods._id === card_factory.info.buy_goods_id) {
							return !!goods_and_num.num
						}
					})) {
					//无需检查是否通过购买途径。而是这个函数的调用者需要对订单信息做判断
					var card = card_factory.toCard(this._id);
					this._getCardIdList().unshift(card._id);
					return card;
				} else {
					throwCusError("订单信息有误，会员卡领取失败")
				}
			} else {
				throwCusError("订单未完成支付，无法领取会员卡")
			}

		},
		getCardFactoryGoodsListFromBus_PageInfo: function(bus_id, num, page) {
			var goods_list = [];
			this.getCardList().forEach(function(card) {
				if (card.state == CardCon.STATE.可用 && card.bus_id == bus_id) {
					goods_list = goods_list.concat(card.card_factory.getGoodsList());
				}
			});
			var total_num = goods_list.length;
			var result = data_cut.page(goods_list, num, page);
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				goods: result,
				page: +page || 0,
				num: +num || 0
			}
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var CouponCon = require("../coupon/_con");
	var CardCon = require("../card/_con");
	var CardFactoryCon = require("../card_factory/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());